package com.hxzy.commons.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hxzy.commons.model.CmsOrg;
import com.hxzy.commons.service.IAuthService;
import com.hxzy.core.page.entity.PageParams;
import com.hxzy.core.page.entity.Pagination;

/**
 * 机构管理
 * @author Thinkpad
 *
 */
@Controller
@RequestMapping("/auth/org")
public class OrgManageAction {
	@Autowired
	private IAuthService authService;
	/**
	 * 机构列表
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/loadOrgList.do")
	public ModelAndView loadOrgList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String orgName = request.getParameter("orgName");
		Pagination page = authService.loadOrgList(pageParams,orgName);
		
		model.addAttribute("page",page); 
		model.addAttribute("orgName",orgName);  
		return new ModelAndView("/app/module/org/orgList",model); 
	}
	/**
	 * 新增一个机构
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneOrg.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneOrg(CmsOrg orgobj,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("rolecode", orgobj);
		Object existData = authService.getUniqueObject(CmsOrg.class, query);
		if(null != existData){
			return "该角色已存在！";
		}
		orgobj.setIsuse(1l);
		authService.save(orgobj); 
		 
		return "保存成功！";
	}
	/**
	 * 修改一个角色
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneOrg.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneOrg(CmsOrg orgobj,HttpServletRequest request,ModelMap model){ 
		//authService.save(userobj);
		return "修改成功！";
	}
	
	
	/**
	 * 删除角色
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneOrg.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneOrg(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		CmsOrg orgObj = (CmsOrg)authService.findByID(CmsOrg.class, new Long(id));
		orgObj.setIsuse(0l);
		authService.saveObject(orgObj);
		return "删除成功！";
	}
	 
}
